﻿using DatabaseProxy.Attributes;
using System.Reflection;

namespace DatabaseProxy;
public class Tool<T> where T : IDatabaseTable
{
    private static PropertyInfo? _primaryKeyProperty;
    private static string _primaryKeyName = "";
    private static string _tableName = "";
    private static Dictionary<string, PropertyInfo> _columns = new();
    /// <summary>
    /// 数据库表中字段名和实体类属性映射：
    /// 键：数据库字段名
    /// 值：实体类属性
    /// </summary>
    public static Dictionary<string, PropertyInfo> Columns
    {
        get
        {
            var properties = typeof(T).GetProperties();
            if (_columns.Count != properties.Length)
            {
                _columns.Clear();
                foreach (var property in properties)
                {
                    if (property.IsDefined(typeof(ColumnNameAttribute), false))
                    {
                        var attribute = property.GetCustomAttribute<ColumnNameAttribute>();
                        var value = attribute?.Name;
                        _columns.Add(!string.IsNullOrEmpty(value) ? value : property.Name, property);
                    }
                    else
                    {
                        _columns.Add(property.Name, property);
                    }
                }
            }
            return _columns;
        }
    }
    /// <summary>
    /// 主键名
    /// </summary>
    public static string PrimaryKeyName
    {
        get
        {
            if (string.IsNullOrEmpty(_primaryKeyName))
            {
                var property = PrimaryKeyProperty;
                if (property!.IsDefined(typeof(ColumnNameAttribute), false))
                {
                    var attribute = property.GetCustomAttribute<ColumnNameAttribute>();
                    _primaryKeyName = !string.IsNullOrEmpty(attribute?.Name) ? attribute.Name : property.Name;
                }
                else
                {
                    _primaryKeyName = property.Name;
                }
            }
            return _primaryKeyName;
        }
    }
    /// <summary>
    /// 主键属性
    /// </summary>
    public static PropertyInfo? PrimaryKeyProperty
    {
        get
        {
            if (_primaryKeyProperty == null)
            {
                _primaryKeyProperty = typeof(T).GetProperties().FirstOrDefault(x => x.IsDefined(typeof(PrimaryKeyAttribute), false));
            }
            return _primaryKeyProperty;
        }
    }
    /// <summary>
    /// 表名
    /// </summary>
    public static string TableName
    {
        get
        {
            if (string.IsNullOrEmpty(_tableName))
            {
                Type type = typeof(T);
                if (type.IsDefined(typeof(TableNameAttribute), false))
                {
                    var attribute = type.GetCustomAttribute<TableNameAttribute>();
                    _tableName = !string.IsNullOrEmpty(attribute?.Name) ? attribute.Name : type.Name;
                }
                else
                {
                    _tableName = type.Name;
                }
            }
            return _tableName;
        }
    }
}
